package lgq.xuan.httprequest.util.sqlite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.Nullable;

public class PhoneCURD {


    public static SQLiteDatabase db;

    /**
     * 数据插入，已改过
     * @param context
     * @param name
     * @param called
     * @param phone
     * @return
     */
    public static long insert(@Nullable Context context, int uid, String name, String called, String phone) {
        db = new PhoneHelper(context).getWritableDatabase();

        ContentValues phoneValues = new ContentValues();
        phoneValues.put("uid", uid);
        phoneValues.put("name", name);
        phoneValues.put("called", called);
        phoneValues.put("phone", phone);
        long change = db.insert("phone", null, phoneValues);
        return change;
    }

    public static long insert(@Nullable Context context, ContentValues phoneValues) {
        db = new PhoneHelper(context).getWritableDatabase();
        long change = db.insert("phone", null, phoneValues);
        return change;
    }


    public static void delete(@Nullable Context context, int _id) {
        db = new PhoneHelper(context).getWritableDatabase();
        int i = db.delete("phone", "_id = " + _id, null);
    }

    public static void signDelete(@Nullable Context context, int id) {
        db = new PhoneHelper(context).getWritableDatabase();
        ContentValues phoneValues = new ContentValues();
        phoneValues.put("isdelete", 1);
        db.update("phone", phoneValues, "_id = " + id, null);
    }

    public static void update(@Nullable Context context, int pid, String name, String called, String phone) {
        db = new PhoneHelper(context).getWritableDatabase();
        ContentValues phoneValues = new ContentValues();
        phoneValues.put("name", name);
        phoneValues.put("called", called);
        phoneValues.put("phone", phone);

        int i = db.update("phone", phoneValues, "_id=" + pid, null);
    }

    public static void update(@Nullable Context context, int uid, int pid, String name, String called, String phone) {
        db = new PhoneHelper(context).getWritableDatabase();
        ContentValues thePid = new ContentValues();
        thePid.put("cloud_id", pid);
        db.update("phone", thePid,
                "uid = ? and name = ? and called = ? and phone = ?",
                new String[]{Integer.toString(uid), name, called, phone});
    }

    @SuppressLint("Recycle")
    public static Cursor queryAllPhone(@Nullable Context context) {
        db = new PhoneHelper(context).getReadableDatabase();
        Cursor cursor = db.query("phone", new String[]{"_id", "name", "called"}, "isdelete = ?", new String[]{"0"}, null, null, null);
        return cursor;
    }

    @SuppressLint("Recycle")
    public static Cursor queryOncePhone(@Nullable Context context, int uid) {
        db = new PhoneHelper(context).getReadableDatabase();
        Cursor cursor = db.query("phone", new String[]{"_id", "name", "called"}, "uid = ? and isdelete = ?", new String[]{Integer.toString(uid), "0"}, null, null, null);
        return cursor;
    }

    @SuppressLint("Recycle")
    public static Cursor queryOnePhone(@Nullable Context context, int id) {
        db = new PhoneHelper(context).getReadableDatabase();
        Cursor cursor = db.query("phone", null, "uid = ?", new String[]{Integer.toString(id)}, null, null, null);
        return cursor;
    }

    @SuppressLint("Recycle")
    public static Cursor queryOnePhoneAllMsg(@Nullable Context context, int id) {
        db = new PhoneHelper(context).getReadableDatabase();
        Cursor cursor = db.query("phone", null, "_id = ?", new String[]{Integer.toString(id)}, null, null, null);
        return cursor;
    }

    public static Cursor queryAllWillDeletePhone(@Nullable Context context, int id) {
        db = new PhoneHelper(context).getReadableDatabase();
        Cursor cursor = db.query("phone", null, "isdelete != ? and _id = ?", new String[]{"0", Integer.toString(id)}, null, null, null);
        return cursor;
    }

    public static void deleteSigns(@Nullable Context context, int uid) {
        db = new PhoneHelper(context).getWritableDatabase();
        db.delete("phone", "isdelete = ? and uid = ?", new String[]{"1", Integer.toString(uid)});
    }

}
